﻿
#include "CollectPicture.h"

using namespace std;
using namespace cv;

int picturenum = 0;//thread exit flag
int saveover = 0;
int startsave = 0;

double CollectPicture::GetClarity(Mat imageGrey)
{
	Mat imageSobel;
	Laplacian(imageGrey, imageSobel, CV_16U);
	//Sobel(imageGrey, imageSobel, CV_16U, 1, 1);
	//图像的平均灰度
	double meanValue = 0.0;
	meanValue = mean(imageSobel)[0];
	cout << meanValue << endl;
	return meanValue;
}

int CollectPicture::GetWhitePixel(Mat imageGrey)
{
	counter = 0;

	model->apply(imageGrey, foregroundMask, doUpdateModel ? -1 : 0);
	maskFrame = foregroundMask.clone();
	threshold(maskFrame, binaryzation, 244, 255, THRESH_BINARY);//THRESH_BINARY 当前点大于阈值时取255(白)，否则设置为0

	namedWindow("binaryzation");
	startWindowThread();
	imshow("binaryzation", binaryzation);
	waitKey(49);


	it = binaryzation.begin<uchar>();
	itend = binaryzation.end<uchar>();

	for (; it != itend; ++it)
	{
		if ((*it) > 0) counter += 1;//二值化后，像素点是0或者255
	}

	return counter;
}

void* CollectPicture::timecount(void* args)
{
	int i = *((int*) args);
	while((picturenum < 16))
	{
		//cout << "i" << i << endl;

		if(!saveover)
		{
		//	cout << "satrt" << endl;

			sleep(i);

		//	cout << "end" << endl;
			startsave = 1;
			saveover = 1;

		}

	}
	cout << "thread exit" << endl;
        picturenum = 0;


}

void CollectPicture::CollectPic(vector<Mat> & pictures)
{		
	pictures.clear();
	string imageFileName;
	stringstream StrStm;

	VideoCapture cap;
	Mat frame;//原图
	Mat imageGrey;

	double Clarity;
	int White;
	int StartFlag = 0;
	int ContinusNum = 0;
	int CorrectPic = 0;
	int PicNum = 0;
	int j = 0;
	double perSec;
	int frameNum;
	int countnum = 0;
	int intervaltime = 5;
	char select;
    int SelectJudge = 0; 

	clock_t start, end;
	cap.open(1);

	if (!cap.isOpened())
	{
		cout << "Can not open video stream " << endl;
		assert(cap.isOpened());
	}

	cout << "默认每隔5秒采集一次图片，是否想更改采集时间间隔？Y/N" << endl;
	cin >> select;
	if (select == 'Y' || select == 'y')
	{
		cout << "设定时间间隔" << endl;
		cin >> intervaltime;
	}
	cout << "每隔" << intervaltime << "s采集一次图像" << endl;
	
	pthread_t tid;
	int ret  = pthread_create(&tid, NULL, timecount, (void*)&intervaltime);

	cout << "ret" << ret <<endl;

	while (pictures.size() < COLLECTNUM)
	{
		
		cap >> frame;
		namedWindow("frame");
		imshow("frame", frame);
		waitKey(30);

		if (startsave)
		{
            cout << "1" << endl;
            if(SelectJudge == 0)
            {
            	char wait;
                cout << "start saving ..." << endl;
				//pictures.push_back(frame.clone());//这里要用frame.clone()不能用frame，这里涉及到push_back是浅拷贝，不复制数据只创建矩阵头，会数据共享，参考https://blog.csdn.net/u011028345/article/details/73183279
				imageFileName.clear();
				StrStm.clear();
				StrStm >> imageFileName;
				imageFileName = "color.jpg";
				imwrite(imageFileName, frame.clone());
				cout << "save the color image" << endl;

				saveover = 0;
				startsave = 0;
				SelectJudge = 1;
				cin >> wait;
            }
            else
            {
                cout << "3" << endl;
				picturenum++;
				cout << "start saving ..." << endl;
				pictures.push_back(frame.clone());//这里要用frame.clone()不能用frame，这里涉及到push_back是浅拷贝，不复制数据只创建矩阵头，会数据共享，参考https://blog.csdn.net/u011028345/article/details/73183279
				imageFileName.clear();
				StrStm.clear();
				StrStm << ++j;
				StrStm >> imageFileName;
				imageFileName += "_dd.jpg";
				imwrite(imageFileName, pictures.at(pictures.size()-1));
				cout << "save the #" << ++PicNum << "image" << endl;
		
				saveover = 0;
				startsave = 0;
            }



		}




	}
	picturenum++;

	//cvStartWindowThread();
	for (int i = 0; i < pictures.size(); i++)
	{
		namedWindow("Collect picture");
		//startWindowThread();
		imshow("Collect picture", pictures[i]);
		waitKey(500);
		destroyWindow("Collect picture");
	}
    //destroyWindow("Collect picture");
    destroyAllWindows();
}
